<?php

namespace app\common\model;

use think\Model;


class Policy extends Model
{

    

    

    // 表名
    protected $name = 'policy';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = "create_time";
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'main_type_text',
        'status_text',
        'create_time_text'
    ];
    

    
    public function getMainTypeList()
    {
        return ['1' => __('Main_type 1'), '2' => __('Main_type 2')];
    }

    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2')];
    }


    public function getMainTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['main_type']) ? $data['main_type'] : '');
        $list = $this->getMainTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function cash()
    {
        return $this->belongsTo('Cash', 'cash_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function flow()
    {
        return $this->belongsTo('Flow', 'flow_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


//    public function profit()
//    {
//        return $this->belongsTo('Profit', 'profit_id', 'id', [], 'LEFT')->setEagerlyType(0);
//    }


    public function stardard()
    {
        return $this->belongsTo('Stardard', 'stardard_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
